package com.xyzboom.web.filters;

import com.xyzboom.database.entity.User;
import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;


@WebFilter(filterName = "LoginFilter", urlPatterns = "/pages/course/*")
public class LoginFilter implements Filter {
    public void init(FilterConfig config) {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (request instanceof HttpServletRequest) {
            User user = (User) ((HttpServletRequest) request).getSession().getAttribute("user");
            if (user == null) {
                response.getWriter().println("您需要登录");
                return;
            }
        }
        chain.doFilter(request, response);
    }
}
